package com.hndfsj.app.datav;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/yygl")
public class YunyinController extends DataVBaseController {
	static Logger log = LoggerFactory.getLogger(YunyinController.class);

	/**
	 * carFlow
	 * 
	 * @param type
	 *            客车,货车,ETC,绿通车
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/day/car/flow")
	public void dayCarFlow(@RequestParam String type, HttpServletResponse response) {
		String url = "/day/car/flow";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
//			parseServerData2JsonString(url);
		}
		String result1 = "{\"客车\":[  {\"value\": \"15342\", \"url\": \"\" }],\"货车\":[  {\"value\": \"15342\", \"url\": \"\" }],"
				+ "\"ETC\":[  {\"value\": \"15342\", \"url\": \"\" }],"
				+ "\"绿通车\":[  {\"value\": \"15342\", \"url\": \"\" }]}";
		writeJson(response, JSON.parseObject(result1).getString(type));
	}

	/**
	 * 日通行量 对比数据及图标
	 * 
	 * @param type
	 * @param img
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/day/car/flow/contrast")
	public void carFlowContrast(@RequestParam String type, String img, HttpServletResponse response) {
		String url = "/day/car/flow";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 5)) {
			// String jsonDate1 =parseServerData2JsonString(url + "?date=" +
			// DateUtils.formatDate(new Date()));
			// String jsonDate2 =parseServerData2JsonString(url + "?date=" +
			// DateUtils.formatDate(new DateTime().minusDays(1).toDate()));
			String jsonDate1 = getData(url);
			String jsonDate2 = "{" + 
					"		\"客车\": [" + 
					"			{" + 
					"				\"value\": \"15942\"," + 
					"				\"url\": \"\"" + 
					"			}" + 
					"		]," + 
					"		\"货车\": [" + 
					"			{" + 
					"				\"value\": \"13842\"," + 
					"				\"url\": \"\"" + 
					"			}" + 
					"		]," + 
					"		\"ETC\": [" + 
					"			{" + 
					"				\"value\": \"14342\"," + 
					"				\"url\": \"\"" + 
					"			}" + 
					"		]," + 
					"		\"绿通车\": [" + 
					"			{" + 
					"				\"value\": \"16342\"," + 
					"				\"url\": \"\"" + 
					"			}" + 
					"		]" + 
					"	}";
			result = "{\"date1\":" + jsonDate1 + ",\"date2\":" + jsonDate2 + "}";
			putCache(url, result);
		}
		float contrast = contrast(result, type);
		if (StringUtils.isBlank(img)) {
			writeJson(response, "[{\"value\": \"" + Math.abs((int)contrast) + "%\",\"url\": \"\"}]");
		} else {
			if (contrast > 0) {// 上升
				writeJson(response,
						"[{\"img\": \"https://datav.oss-cn-hangzhou.aliyuncs.com/uploads/images/9b139a53a27b8d13c4473220940a5d5f.png\",\"url\": \"\"}]");
			} else {// 下降
				writeJson(response,
						"[{\"img\": \"https://datav.oss-cn-hangzhou.aliyuncs.com/uploads/images/bb6d0a8799ac79788149140e9dbd16eb.png\",\"url\": \"\"}]");
			}
		}
	}

	private float contrast(String result, String key) {
		int num1 = JSON.parseObject(result).getJSONObject("date1").getJSONArray(key).getJSONObject(0)
				.getIntValue("value");
		int num2 = JSON.parseObject(result).getJSONObject("date2").getJSONArray(key).getJSONObject(0)
				.getIntValue("value");
		return (num1 - num2) * 1f / num2 * 100;
	}

	/**
	 * 车流量（站）
	 * 
	 * @param site
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/site/car/flow")
	public void siteCarFlow(String site, HttpServletResponse response) {
		String url = "/site/car/flow";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 5)) {
			// result =parseServerData2JsonString(url + "?date=" + DateUtils.formatDate(new
			// Date()));
			result = getData(url);
			putCache(url, result);
		}
		if (StringUtils.isNotBlank(site)) {
			JSON.parseArray(result).forEach(object -> {
				JSONObject jsonObject = ((JSONObject) object);
				if (site.equals(jsonObject.getString("name"))) {
					writeJson(response, "[" + jsonObject.toJSONString() + "]");
				}
			});
		} else {
			writeJson(response, result);
		}
	}

	/**
	 * 客货数量数据
	 * 
	 * @param type
	 *            客车,货车
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/day/car/contrast")
	public void dayCarContrast(HttpServletResponse response) {
		String url = "/day/car/flow";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result =parseServerData2JsonString(url + "?date=" + DateUtils.formatDate(new
			// Date()));
			result = getData(url);
			putCache(url, result);
		}
		String json = "[ { \"type\": \"客车\", \"value\": "
				+ JSON.parseObject(result).getJSONArray("客车").getJSONObject(0).getString("value")
				+ " }, { \"type\": \"货车\", \"value\": "
				+ JSON.parseObject(result).getJSONArray("货车").getJSONObject(0).getString("value") + " } ]";
		writeJson(response, json);
	}

	/**
	 * 进/入口车辆数据
	 * 
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/day/in/out")
	public void dayInOut(HttpServletResponse response) {
		String url = "/day/in/out";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 5)) {
			// result =parseServerData2JsonString(url + "?date=" + DateUtils.formatDate(new
			// Date()));
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, result);
	}

	/**
	 * 车流量趋势
	 * 
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/car/flow/trend")
	public void carFlowTrend(HttpServletResponse response) {
		String url = "/car/flow/trend";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 5)) {
			// result =parseServerData2JsonString(url + "?date=" + DateUtils.formatDate(new
			// Date()));
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, result);
	}
	/**
	 * 各站年度任务完成率
	 * @param response
	 * zxj
	 */
	@RequestMapping(value = "/site/task/complete/rate")
	public void taskCompleteRate(HttpServletResponse response,@RequestParam String site) {
		String url = "/site/task/complete/rate";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 5)) {
			// result =parseServerData2JsonString(url + "?date=" + DateUtils.formatDate(new
			// Date()));
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, "["+JSON.parseObject(result).getString(site)+"]");
	}
	@RequestMapping(value = "/exceptional/case/trend")
	public void exceptionalCaseTrend(HttpServletResponse response,String type) {
		String url = "/exceptional/case/trend";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 5)) {
			// result =parseServerData2JsonString(url + "?date=" + DateUtils.formatDate(new
			// Date()));
			result = getData(url);
			putCache(url, result);
		}
		if("征改免".equals(type)) {
			writeJson(response, JSON.parseArray(result).getJSONArray(1).toJSONString());
		}else {
			writeJson(response, JSON.parseArray(result).getJSONArray(0).toJSONString());
			
		}
	}
	
}
